package chapter14.section2;
// 程序14-7：菜单程序
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

public class MenuDemo extends JFrame {
	public MenuDemo(String title,int width,int height)  {
		super(title);
		this.setSize(width, height);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setContentPane(new JPanel());
		initMenu();
	}
	private void initMenu(){
		//创建菜单栏对象
		JMenuBar mnubar=new JMenuBar();
		
		//创建第一个菜单,并将它追加到菜单栏中
		JMenu mnuFirst =new JMenu("文件");
		mnubar.add(mnuFirst);
		//创建一个菜单项，并将它追加到菜单mnuFirst中
		JMenuItem itmAdd = new JMenuItem("添加");
		mnuFirst.add(itmAdd);
		mnuFirst.addSeparator();//增加菜单分隔符
		//创建一个菜单项，并将它追加到菜单mnuFirst中
		JMenuItem itmExit = new JMenuItem("退出(E)");
		mnuFirst.add(itmExit);
		itmExit.setMnemonic(KeyEvent.VK_E);
//设置快捷键，这里是同时按下CTRL+E键，相当于选中该菜单项
		itmExit.setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_E,InputEvent.CTRL_DOWN_MASK)); 
		//创建第二个菜单,并将它追加到菜单栏中
		JMenu mnuSecpmd =new JMenu("Help");
		mnubar.add(mnuSecpmd);
		//将mnuBar设置为当前窗口的菜单栏对象
		this.setJMenuBar(mnubar);
	}
	public static void main(String[] args) {
		MenuDemo app=new MenuDemo("MenuDemo",600,400);
		app.setVisible(true);
	}
}